<?php
function gen_password($password){
    return md5($password);
}
class Api{
    function get($req){
        $id = $req->param;
        $row = pt\db\Mysql::init()->row("select *,id as `key` from user where id = ?",$id);
        $row['password'] = "";
        return $row;
    }
    function post($req){
        $name = $req->body->name;
        $mobile = $req->body->mobile;
        $status = $req->body->status;
        $role_id = $req->body->role_id;
        $email = $req->body->email;
        $password = $req->body->password;

        if(!$name || strlen($name) < 2){
            throw new ErrHttp("姓名至少2位",500);
        }
        if(!$mobile || strlen($mobile) != 11){
            throw new ErrHttp("手机号需为11位",500);
        }
        $res = pt\db\Mysql::init()->row("select id from user where mobile = ?",$mobile);
        if($res){
            throw new ErrHttp("手机号存在",500);
        }
        if(!$password || strlen($password) < 8){
            throw new ErrHttp("密码至少8位",500);
        }
        $password = gen_password($password);
        $row = array(
            "name"=>$name,
            "password"=>$password,
            "mobile"=>$mobile,
            "status"=>$status ? true:false,
            "add_time"=>date("Y-m-d H:is")
        );

        if($email){
            $row['email'] = $email;
        }
        if($role_id){
            $row['role_id'] = $role_id;
        }
        $id = pt\db\Mysql::init()->insert("user",$row);
        $res = array(
            "id"=>$id
        );
        return $res;
    }
    function put($req){
        $id = $req->param;

        $name = $req->body->name;
        $mobile = $req->body->mobile;
        $status = $req->body->status;
        $role_id = $req->body->role_id;
        $email = $req->body->email;
        $password = $req->body->password;

        if(!$name || strlen($name) < 2){
            throw new ErrHttp("姓名至少2位",500);
        }
        if(!$mobile || strlen($mobile) != 11){
            throw new ErrHttp("手机号为11位",500);
        }
        $res = pt\db\Mysql::init()->row("select id from user where mobile = ? and id <> ?",$mobile,$id);

        if($res){
            throw new ErrHttp("手机号存在",500);
        }


        $row = array(
            "name"=>$name,
            "mobile"=>$mobile,
            "status"=>$status ? true:false,
        );
        if($password && strlen($password) < 8){
            throw new ErrHttp("密码至少8位",500);
        }
        if($password){
            $password = gen_password($password);
            $row['password'] = $password;
        }
        if($email){
            $row['email'] = $email;
        }
        if($role_id){
            $row['role_id'] = $role_id;
        }
        pt\db\Mysql::init()->update("user",$row,array("id"=>$id));
        $res = array(
            "message"=>"修改成功"
        );
        return $res;
    }
    function delete($req){
        $id = $req->param;
        if($id == 1) throw new ErrHttp("不参删除创建者",500);
        pt\db\Mysql::init()->delete("user",array("id"=>$id));
        $res = array(
            "message"=>"删除成功"
        );
        return $res;
    }
}
